<template>
  <div>
    <el-form class="elForm" :inline="true" :model="dataForm" :rules="dataRule" ref="dataForm">
      <el-form-item>
        <el-button size="medium" class="add-btn" v-if="dataListSelections.length==0" @click="addOrUpdateHandle()">
          新建标准
        </el-button>
        <el-button size="medium" type="danger" v-if="dataListSelections.length!=0" @click="deleteHandle()">
          批量删除
        </el-button>
      </el-form-item>
      <el-form-item prop="title">
        <el-input v-model="dataForm.title" placeholder="标准名称" size="medium" class="input" clearable="clearable" />
        <el-button class="search" size="small" @click="searchHandle()" icon="el-icon-search"></el-button>
      </el-form-item>
    </el-form>
    <el-table 
      :data="dataList" 
      ref="dataList"
      border v-loading="dataListLoading" 
      @selection-change="selectionChangeHandle" 
      style="width: 100%;" 
      size="medium" 
      :header-cell-style="{borderColor: 'rgb(235,235,235)',background: 'rgb(247,247,247)'}" 
      :cell-style="{padding: '8px 0',borderColor: 'rgb(235,235,235)'}" 
      class="elTable"
      :row-key="getRowKeys"
      >
      <el-table-column type="selection" :selectable="selectable" header-align="center" align="center" width="50" :reserve-selection="true" />
      <el-table-column prop="title" label="标准名称" min-width="510" />
      <el-table-column prop="sort" header-align="center" align="center" label="排序号" min-width="100" />
      <el-table-column prop="create_time" header-align="center" align="center" label="创建时间" min-width="175" />
      <el-table-column header-align="center" align="center" width="80" label="操作">
        <template #default="scope">
          <el-dropdown>
            <span class="el-dropdown-link">
              <i class="el-icon-more cursor" style="font-size: 28px"></i>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="addOrUpdateHandle(scope.row.id)">编辑</el-dropdown-item>
                <el-dropdown-item @click="deleteHandle(scope.row.id)" :disabled="dataListSelections.length!=0">删除</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination 
      @size-change="sizeChangeHandle" 
      @current-change="currentChangeHandle" 
      :current-page="pageIndex" 
      :page-sizes="[10, 20, 50]" 
      :page-size="pageSize" 
      :total="totalCount" layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>

  </div>
</template>

<script>
export default {
  data: function() {
    return {
      dataForm: {
        title: null
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalCount: 0,
      dataListLoading: false,
      dataListSelections: [],
    }
  },
  methods: {
    // 删除角色
    deleteHandle(id) {
      let ids = id ? [id] : this.dataListSelections.map(item => item.id)
      // console.log(ids)

      if (ids.length == 0) {
        this.$message({
          message: '没有选中记录',
          type: 'warning',
          duartion: 1200
        })
      } else {
        this.$confirm('确定要删除选中记录？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http('rank/deleteRanks', 'post', { ids }, true, resp => {
            console.log(resp)
            if (resp.code == 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duartion: 1200
              })
              // 清除table选中
              this.$refs.dataList.clearSelection()
              this.loadDataList()
            } else {
              this.$message({
                message: '未能删除记录',
                type: 'warning',
                duartion: 1200
              })
            }
          })
        })
      }
    },
    // 新增 编辑 用户
    addOrUpdateHandle(id) {
      if (id) {
        this.$router.push({ name: 'ExamRankUpdate', params: { id } })
      } else {
        this.$router.push({ name: 'ExamRankAdd' })
      }
    },
    // 查询功能
    searchHandle() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$refs['dataForm'].clearValidate()
          // 后端接收到''会报错，接收null是代表没有这个条件
          if (this.dataForm.title == '') {
            this.dataForm.title = null
          }
          if (this.pageIndex != 1) {
            this.pageIndex = 1
          }
          this.loadDataList()
        } else {
          return false
        }
      })
    },
    // 加载分页数据
    loadDataList() {
      this.dataListLoading = true
      let data = {
        title: this.dataForm.title,
        page: this.pageIndex,
        length: this.pageSize
      }
      // console.log(data);
      
      this.$http('rank/selectRankBypages', 'post', data, false, resp => {
        let page = resp.page
        console.log(page)
        this.dataList = page.list
        this.dataList.forEach(item => {
          // 处理时间中间的T 如：2021-11-14T19:11:51
          item.create_time = item.create_time.replace('T', ' ')
        })
        this.totalCount = page.totalCount
        this.dataListLoading = false
      })
    },
    // 分页一页数量改变时触发
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.loadDataList()
    },
    // 分页换页时触发
    currentChangeHandle(val) {
      this.pageIndex = val
      this.loadDataList()
    },
    selectionChangeHandle: function(val) {
      this.dataListSelections = val
    },
    selectable: function(row, index) {
      // console.log(row)
      if (row.systemic || row.users > 0) {
        return false
      }
      return true
    },
    // 设置每页显示记录数，并显示第一页
		sizeChangeHandle(val) {
			this.pageSize = val
			this.pageIndex = 1
			this.loadDataList()
		},
		// 跳转到第几页
		currentChangeHandle(val) {
			this.pageIndex = val
			this.loadDataList()
    },
    // 保存选中的数据id,row-key就是要指定一个key标识这一行的数据
    getRowKeys(row) {
      return row.id
    },
  },
  created() {
    this.loadDataList()
  }
}
</script>

<style lang="scss" scoped>
@import './examRank.scss';
</style>
